package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;
import common.utils.PromptUtil;


public class SetGenderDialog2 extends Dialog{
    private TextView resultTv;
    private int currentGender;
    private CommitDialogCallBack callback;
    private RadioButton setgender_girl_radio_btn;
    private RadioButton setgender_boy_radio_btn;
    private String male;
    private String female;

    private int dialogWidth;

    /**
     *
     * @param resultTv
     * 修改完成后，显示结果的textview
     */
    public SetGenderDialog2(Context context, int currentGender, TextView resultTv, CommitDialogCallBack callback){
        super(context);
        this.resultTv = resultTv;
        this.currentGender = currentGender;
        this.callback = callback;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_setgender2);

        male = getContext().getString(R.string.male);
        female = getContext().getString(R.string.female);

        setDialogAttributes();
        initDialogView();
    }

    private void initDialogView() {
        setgender_girl_radio_btn = (RadioButton)findViewById(R.id.setgender_girl_radio_btn);
        RadioGroup.LayoutParams layoutParams = (RadioGroup.LayoutParams) setgender_girl_radio_btn.getLayoutParams();
        layoutParams.leftMargin = (dialogWidth - DensityUtil.dip2px(getContext(), 264))/3 ;
        setgender_girl_radio_btn.setLayoutParams(layoutParams);
        setgender_girl_radio_btn.setBackgroundResource(R.drawable.selector_setgender_girl_bg2);

        setgender_boy_radio_btn = (RadioButton)findViewById(R.id.setgender_boy_radio_btn);
        RadioGroup.LayoutParams layoutParams2 = (RadioGroup.LayoutParams) setgender_boy_radio_btn.getLayoutParams();
        layoutParams2.leftMargin = layoutParams.leftMargin;
        setgender_boy_radio_btn.setLayoutParams(layoutParams2);
        setgender_boy_radio_btn.setBackgroundResource(R.drawable.selector_setgender_boy_bg2);

        if (currentGender==1){
            setgender_boy_radio_btn.setChecked(true);
        }else if (currentGender==2){
            setgender_girl_radio_btn.setChecked(true);
        }


        //初始化确定，取消按钮
        findViewById(R.id.cancel_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sure_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (setgender_boy_radio_btn.isChecked()||setgender_girl_radio_btn.isChecked()){
                        resultTv.setText(setgender_boy_radio_btn.isChecked()?male:female);
                        callback.onCommit();
                        dismiss();
                    }else{
                        //一个都没有选择
                        PromptUtil.toast(getContext(),R.string.set_gender);
                    }
                }
                return false;
            }
        });
    }


    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        dialogWidth = lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }


}
